<template>
  <div>
    <div class="regTop">
      <van-nav-bar title="登录" left-arrow @click-left="$router.back()" />
    </div>
    <div class="reg-main">
      <!-- <van-cell-group> -->
      <van-form @submit="onSubmit">
        <van-field
          v-model="username"
          name="username"
          label="用户名"
          placeholder="请输入用户名"
          :rules="[{ required: true, message: '请填写用户名' }]"
        />

        <van-field
          v-model="password"
          name="password"
          label="密码"
          placeholder="请输入密码"
          :rules="[{ required: true, message: '请输入密码' }]"
        />

        <div style="margin: 16px">
          <van-button
            round
            block
            type="info"
            color="rgb(7,193,96)"
            native-type="submit"
            >登录</van-button
          >
        </div>
        <div class="footer">
 <span >还没有账号点击<em @click="$router.push('reg')">注册  </em></span>
 <span>忘记密码?</span>
        </div>
       
      </van-form>
      <!-- </van-cell-group> -->
    </div>
  </div>
</template>

<script>
import { NavBar, Field, Button, Form } from "vant";
export default {
  data() {
    return {
      username: "",
      password: "",
   
    };
  },
  components: {
    [NavBar.name]: NavBar,
    [Field.name]: Field,
    [Button.name]: Button,
    [Form.name]: Form,
  },
  created() {
    // this.getReg()
  },
  methods: {
    onSubmit(values) {
      let username = this.username;
      let password = this.password;

      console.log(typeof username);
      console.log("submit", values);

      this.$http
        .post("/user/login", {
          username,
          password,
        })
        .then((res) => {
          if (res.code == 0) {
             this.$toast("登录成功");
             let { token, tokenExpired, uid ,userInfo} = res;

          localStorage.setItem("tt-token", token);
          localStorage.setItem("tt-tokenExpired", tokenExpired);
          localStorage.setItem("tt-uid", uid);

          localStorage.setItem('tt-userInfo',JSON.stringify(userInfo)) //  昵称 头像这些放在这


        
          this.$root.userInfo = userInfo
          this.$root.token = token
          this.$root.uid = uid
            setTimeout(() => {
              this.$router.push("my");
            }, 2000);
          } else {
             this.$toast(res.msg || "登陆失败");
          }
        });
    },
  },
};
</script>

<style lang="scss" scoped>
.footer{
  padding: 0 16px;
  display: flex;
  justify-content: space-between;
  em{
    font-style:normal ;
    color: cornflowerblue;
  }
}
</style>